// pages/search/search.js
let app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    placeholder: '',
    focus: true,
    value: "",
    history: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.placeholder=='可搜索购买者手机号'){
      this.setData({
        placeholder: options.placeholder
      })
    }else{
      this.setData({
        value: options.placeholder
      })
    }
    
    let history = wx.getStorageSync('searchs')
    if (history) {
      history = history?.split(',').filter((item) => {
        return item
      })
      history = new Set([...history])
      this.setData({
        history: [...history]
      })
    }
  },

  bindtapHistory(e) {
    this.setData({
      value: e.currentTarget.dataset.value,
      focus: true
    })
    this.bindconfirm()
  },
  bindinput(e) {
    this.setData({
      value: e.detail.value
    })
  },
  bindsclos(){
    this.setData({
      value: '',
      placeholder:'可搜索购买者手机号'
    })
    app.globalData.search = this.data.value
    app.emit('search', this.data.value)
    wx.navigateBack({
      delta: 1,
    })
  },
  bindtapDelete () {
    wx.removeStorageSync('searchs')
    this.setData({
      history: []
    })
  },
  bindconfirm() {
    let searchs = wx.getStorageSync('searchs')
    searchs = searchs + ',' + this.data.value
    wx.setStorageSync('searchs', searchs)
    app.globalData.search = this.data.value
    app.emit('search', this.data.value)
    wx.navigateBack({
      delta: 1,
    })
  }
})